package com.joysuch.wwyt.workflow.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.workflow.bean.WorkFlowFormTemplateBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowFormTemplateListBean;
import com.joysuch.wwyt.workflow.bean.WorkFlowFormTemplateQueryBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowDoc;
import com.joysuch.wwyt.workflow.entity.WorkFlowFormTemplate;
import com.joysuch.wwyt.workflow.service.WorkFlowFormTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/workflow/form")
@Api(tags = "业务流程文档引擎-表单模板配置管理")
@CrossOrigin
public class WorkFlowFormTemplateController {

    @Autowired
    private WorkFlowFormTemplateService formModuleService;



    @PostMapping("/page")
    @ApiOperation(value = "获取表单模板配置列表(分页）" , response = WorkFlowFormTemplate.class)
    public ResultBean page(@RequestBody  WorkFlowFormTemplateQueryBean bean) {
       Pageable pageable = QPageRequest.of(bean.getPage(), bean.getSize());
       pageable= PageDataUtils.addOrderByDesc(pageable,"id");
       Page<WorkFlowFormTemplateBean> list = formModuleService.pageList(bean, pageable);
       return ResultBean.pageData(list);
    }


    @GetMapping("/getById")
    @ApiOperation(value = "根据Id查询表单模板配置" , response = WorkFlowFormTemplate.class)
    public ResultBean getById(@RequestParam Long id) {
        WorkFlowFormTemplate formTemplate = formModuleService.findById(id);
        return ResultBean.success(formTemplate);
    }

    @RequiresPermissions("workflow:form:delete")
    @GetMapping("/deleteById")
    @ApiOperation(value = "根据Id删除表单模板配置")
    public ResultBean deleteById(@RequestParam Long id) {
        formModuleService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @RequiresPermissions("workflow:form:save")
    @PostMapping("/save")
    @ApiOperation(value = "保存表单模板配置" , response = WorkFlowFormTemplate.class)
    public ResultBean save(@RequestBody WorkFlowFormTemplateBean bean) {
        return formModuleService.save(bean);
    }


}
